package com.welse.modulebiz.controller;

import com.welse.modulebiz.domain.Result;
import com.welse.modulebiz.domain.dto.GetEmailCode;
import com.welse.modulebiz.service.MailService;
import com.welse.modulebiz.utils.CodeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.TimeUnit;

@RequestMapping("/emailCode")
@RestController
public class emailcode {

    @Autowired
    private MailService mailService;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @PostMapping
    public Result emailCode(GetEmailCode getEmailCode){
        String email = getEmailCode.getEmail();
        String code = CodeUtil.creatCode(6);
        //发送邮件
        mailService.sendCodeMailMessage(email,code);
        //保存验证码
        stringRedisTemplate.opsForValue().set("email:code:" + email, code, 10, TimeUnit.MINUTES);

        return Result.success();
    }
}
